﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using LMKJ.Classes;
using System.Text.RegularExpressions;

namespace LMKJ
{
    public partial class searchvolunteers : System.Web.UI.Page
    {
        private static ArrayList volunteerList = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                volunteerList = DatabaseManager.RetrieveAllVolunteer();

                volunteerDlist.DataSource = volunteerList;
                volunteerDlist.DataBind();
            }
        }

        protected void searchBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (searchDdl.SelectedIndex == 0)
                {
                    volunteerList.Clear();
                    volunteerList.Add(DatabaseManager.RetrieveVolunteerById(Convert.ToInt32(searchTbx.Text)));
                }
                else if (searchDdl.SelectedIndex == 1)
                {
                    volunteerList = DatabaseManager.RetrieveVolunteersByName(searchTbx.Text);
                }

                volunteerDlist.DataSource = volunteerList;
                volunteerDlist.DataBind();
            }
        }

        protected void showAllBtn_Click(object sender, EventArgs e)
        {
            volunteerList = DatabaseManager.RetrieveAllVolunteer();

            volunteerDlist.DataSource = volunteerList;
            volunteerDlist.DataBind();
        }

        protected void searchValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            Regex NumberOnly = new Regex("\\d+$");

            if (searchDdl.SelectedIndex == 0)
            {
                if (NumberOnly.IsMatch(searchTbx.Text))
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false;
                }
            }
            else
            {
                args.IsValid = true;
            }
        }
    }
}